package com.petter.jvm.gc;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : GD
 * @date :2021/12/10 : 14:22
 */
public class STWTest {


    public static class PrintWorkThread extends  Thread{
        @Override
        public void run() {
            long tag = System.currentTimeMillis();
            while(true){
                System.out.println(((double) (System.currentTimeMillis() - tag))/1000);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static class GcWorkThread extends  Thread{
        List list = new ArrayList<>();
        @Override
        public void run() {

            int i = 0;
            while(true){
                byte[] bytes = new byte[1024*512*1];
                list.add(bytes);
                i++;
                if(i == 100){
                    list.clear();
                    System.gc();
                    i=0;
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        PrintWorkThread thread = new PrintWorkThread();
        thread.start();

        GcWorkThread thread1 = new GcWorkThread();
        thread1.start();
    }
}
